﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Service.Impl;
using System.Collections;

namespace DAL.Service.admin.Impl
{
    public class RoleServiceImpl : BaseServiceImpl, IRoleService
    {

        public IList getRoleList()
        {
            MyQuery query = new MyQuery();
            query.setQueryString("SELECT R_id,R_Name FROM cms.Role");
            return this.getList(query);
        }

        public int deleteRole(string ids)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("DELETE FROM cms.Role WHERE R_id in(" + ids + ")"); ;
            return base.exeUpdateSql(query);
        }

        public IList getRolebyid(int id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("SELECT * FROM cms.Role WHERE R_id=@id");
            query.addParas("@id", id);
            return base.getList(query);
        }


        public IList getRoleList(int pageno, int pagesize)
        {
            MyQuery query = new MyQuery();
            StringBuilder sb = new StringBuilder();
            if (pageno == 1)
            {
                sb.Append("SELECT count(*) as total FROM cms.Role;");
                sb.Append("select top " + pagesize + " * from cms.Role  order by R_id desc;");
            }
            else
            {
                sb.Append("select * from(");
                sb.Append("select *,ROW_NUMBER() OVER (order by R_id desc) as rno from  cms.Role");
                sb.Append(") as a where a.rno>@start and a.rno<@end  order by R_id desc;");
                query.PageNo = pageno;
                query.PageSize = pagesize; ;
            }
            query.setQueryString(sb.ToString());
            return base.getList(query);
        }

        public int getCountByName(string name,int id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString(" select count(1) as total from cms.Role where R_Name='" + name + "'" + " and R_id != " + id);
            Hashtable ht = (Hashtable)this.getList(query)[0];
            return Convert.ToInt32(ht["total"]);
        }
    }
}
